<?php
namespace app\api\model;

use app\common\logic\UserLevelLogic;
use think\Model;

class SendgoodsModel extends Model
{
    protected $name = 'sendgoods';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $append = [
        'createtime_text',
    ];
    public function getImageAttr($var,$data)
    {
        if(!empty($var)){
            return cdnurl($var);
        }
        return '';
    }

    public function getCreatetimeTextAttr($val,$data)
    {
      return date('Y-m-d',$data['updatetime']);
    }

    //赠送入会礼遇
    public static function sendGoods($user)
    {
        //var_dump($user['member_level']);
        if($user['member_level'] <= 0){
            return false;
        }
        $userLevel = UserLevelLogic::getCurLevelByUserId($user['member_level']);
        $map = [
            'user_id' => $user['id'],
            'level' => $user['member_level'],
            'sendtype_id' => 2,
        ];
        $row = self::where($map)->find();
        if(empty($row)){
            $map = [
                'title' => $userLevel['name'].'入会礼遇',
                'user_id' => $user['id'],
                'level' => $user['member_level'],
                'sendtype_id' => 2,
            ];
            self::create($map);
        }
    }
}
